package com.sun.springbootfiledownload.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @version 1.0
 * @Package: com.sun.springbootfiledownload.config
 * @ClassName: AsyncConfig
 * @Author: sgq
 * @Date: 2025/4/10 14:24
 * @Description: TODD
 */
@Configuration
@EnableAsync
public class AsyncConfig {

    /**
     * 配置异步任务线程池
     * @return 线程池执行器
     */
    @Bean(name = "fileTaskExecutor")
    public Executor fileTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(5);
        // 最大线程数
        executor.setMaxPoolSize(10);
        // 队列容量
        executor.setQueueCapacity(50);
        // 线程名前缀
        executor.setThreadNamePrefix("FileTask-");
        // 初始化
        executor.initialize();
        return executor;
    }
}